import Vue from "vue";
import talent from '@/assets/img/superman.png'; //图片
import liveVideo from '@/assets/img/live-video.png'; //图片
import smallShop from '@/assets/img/smallShop.png'; //图片
import shop from '@/assets/img/shop.png'; //图片
import video from '@/assets/img/video.png'; //图片

// // 自定义数值渐变指令
Vue.directive("countRunning", {
  bind: function (el, binding, vnode, oldVnode) {
    var timer = null;
    const targetDomCount = binding.value.num; //targetDomCount为目标值
    let nowCount = 0; //当前页面显示值
    //console.log('binding', binding)
    if (binding.oldValue != undefined) {
      nowCount = binding.oldValue.num;
    }
    let step = Math.floor(targetDomCount / binding.value.step);
    if (targetDomCount < 10) {
      if (targetDomCount == 0) {
        el.innerHTML = targetDomCount;
      } else {
        let loop = setInterval(() => {
          el.innerHTML = Math.abs(Math.ceil(Math.random() * 10 - 1));
        }, 180);
        setTimeout(() => {
          clearInterval(loop);
          el.innerHTML = targetDomCount;
        }, 3000);
      }
    } else {
      /*每30ms增加的数值--*/
      timer = setInterval(() => {
        nowCount += step;
        if (nowCount > targetDomCount) {
          clearInterval(timer);
          timer = null;
          nowCount = targetDomCount;
        }
        el.innerHTML = nowCount;
      }, binding.value.time);
    }
  }
});
// ====加千位符并且前面补0
// function num_comma(parm) {
//   let num = (parm || 0).toString(),
//     result = "";
//   while (num.length > 3) {
//     result = "<span>,</span>" + num.slice(-3) + result;
//     num = num.slice(0, num.length - 3);
//   }
//   if (num) result = num + result;
//   return result;
// }
// 小数点钱每三位用,隔开
function num_comma(num) {
  var result = ""
  var arr = []
  var str = num.toString().split('.')[0]
  var count = str.length;
 // console.log('count' + count)

  while (count >= 3) {
    arr.unshift(str.slice(count - 3, count));
    count -= 3;
  }

  // 如果是不是3的倍数就另外追加到上去
  str.length % 3 && arr.unshift(str.slice(0, str.length % 3))
  result = arr.toString()
  // 如果有小数点要添加上
  if (num.toString().indexOf('.') != -1) {
    let dot = '.' + num.toString().split('.')[1]
    result = arr.toString() + dot
  }

  return result;
}
Vue.directive("num_running", {
  bind: function (el, binding, vnode, oldVnode) {
    const target_num = binding.value.num;
    let now_num = 0; //当前页面显示值
    let step = Math.floor(target_num / 10);
    if (target_num < 50) {
      let result = target_num;
      el.innerHTML = num_comma(result);
    } else {
      now_num += step;
      el.innerHTML = num_comma(now_num);
      let loop = setInterval(() => {
        now_num += step;
        if (now_num > target_num) {
          clearInterval(loop);
          el.innerHTML = num_comma(target_num);
        } else {
          el.innerHTML = num_comma(now_num);
        }
      }, 150);
    }
  }
});
//  v-title data-title="首页"
Vue.directive("title", {
  //单个修改标题
  inserted: function (el, binding) {
    document.title = el.dataset.title;
  }
});
// 自定义图片出错添加默认图片
let imgErrorDirectiveFn = '';
Vue.directive("errorImg",{
    bind(el, binding){
      el.addEventListener('error',imgErrorDirectiveFn = function(){
        if(binding.arg == 'talent'){
          el.src = talent;
        }else if(binding.arg == 'live'){
          el.src = liveVideo;
        }else if(binding.arg == 'smallShop'){
          el.src = smallShop;
        }else if(binding.arg == 'shop'){
          el.src = shop;
        }else if(binding.arg == 'video'){
          el.src = video;
        }
        el.onerror = null;
      });
    },
    unbind(el){
      el.removeEventListener('error',imgErrorDirectiveFn);
    }
});

